import { createStore, applyMiddleware, combineReducers } from 'redux';
import thunk from "redux-thunk"
import storage from "./locastorage"
const initState = {
    userInfo: {}, //用户的信息
    carList: JSON.parse(localStorage.getItem("carList")) || [], //购物车数据
    FavoriteBookList: JSON.parse(localStorage.getItem("FavoriteBookList")) || [], //喜欢的书籍列表
    addressList: [], //点单地址
    currMessage: null,
    messageList:[],
    unpaidList:[], //未支付点单列表
    prepaidList:[], //已支付订单列表
    lastPathname:"",
}

//添加已支付订单
const addprepaidList=state=>{
    let arr = state.carList.filter(item=>{
        return item.ischeck
    })
    let unarr = state.carList.filter(item=>{
        return !item.ischeck
    })
    state.prepaidList.push({pid:"",children:arr,isPay:1})
    state.carList=unarr
    storage("carList",state.carList)
    return JSON.parse(JSON.stringify(state))
}
//添加未支付订单
const addunpaidList=state=>{
    let arr = state.carList.filter(item=>{
        return item.ischeck
    })
    let unarr = state.carList.filter(item=>{
        return !item.ischeck
    })
    state.unpaidList.push({pid:"",children:arr,isPay:2})
    state.carList=unarr
    storage("carList",state.carList)
    return JSON.parse(JSON.stringify(state))
}

const updateUser = (state, user) => {
        console.log("store_updateUser", user);
        return {...state, userInfo: user }
}
//home喜欢列表
const syncFavoriteBookList = (state, user) => {
    storage("FavoriteBookList", user)
    return {...state, FavoriteBookList: user }
}

//购物车数据列表
const getCarList = (state, user) => {
    let index = state.carList.findIndex(item => {
        return item.bid === user.bid
    })
    if (index >= 0) {
        state.carList[index].num += user.num
    } else {
        state.carList.push(user)
    }
    storage("carList", state.carList)
    return JSON.parse(JSON.stringify(state))
}

//购物车数量加减
const distCarList = (state, item) => {
    let index = state.carList.findIndex(ite => {
        return ite.bid === item.bid
    })
    state.carList[index].num--
        if (state.carList[index].num <= 1) {
            state.carList[index].num = 1
        }
    storage("carList", state.carList)
    return JSON.parse(JSON.stringify(state))
}
const addCarList = (state, item) => {
    let index = state.carList.findIndex(ite => {
        return ite.bid === item.bid
    })
    state.carList[index].num++
        storage("carList", state.carList)
    return JSON.parse(JSON.stringify(state))
}

//购物车单选
const getCheck = (state, item) => {
        let index = state.carList.findIndex(ite => {
            return ite.bid === item.bid
        })
        state.carList[index].ischeck = item.ischeck
        storage("carList", state.carList)
        return JSON.parse(JSON.stringify(state))
    }

//购物车全选
const getAllCheck = (state, check) => {
    state.carList.forEach(ite => {
        return ite.ischeck = check
    })
    storage("carList", state.carList)
    return JSON.parse(JSON.stringify(state))
}

//点单地址列表
const getAddressList = (state, data) => {
    let index = state.addressList.findIndex(item => {
        return item.aid === data.aid
    })
    if (index >= 0) {

    } else {
        state.addressList.push(data)
    }
    return JSON.parse(JSON.stringify(state))
}


const pullCMessage = (state, data) => {
    console.log("pullCMessage-->")
    state.currMessage = data;
    return JSON.parse(JSON.stringify(state))
}
const clearCMessage = (state) => {
    console.log("clearCMessage-->")
    state.clearCMessage = null;
    return JSON.parse(JSON.stringify(state))
}
//加入消息队列
const addMessageList = (state, item) => {
    console.log("addMessageList-->")
    if(Array.isArray(item)){
        state.messageList = item;
    }else{
        let target = state.messageList.find(it => it.mid == item.mid)
        if(!target) state.messageList.push(item);
    }
    return JSON.parse(JSON.stringify(state))
}
//清楚队列
const clearMessageList = (state) => {
    console.log("clearMessageList-->")
    state.messageList = []
    return JSON.parse(JSON.stringify(state))
}

// 暂存路由
const pLastPathname = (state, path) =>{
    state.lastPathname = path;
    return JSON.parse(JSON.stringify(state))
}

const reducer = (state = initState, action) => {
    switch (action.type) {
        case "UPDATEUSER":
            return updateUser(state, action.data)
        case "SYNCFAVORITE":
            return syncFavoriteBookList(state, action.data)
        case "CAR":
            return getCarList(state, action.data)
        case "DIST":
            return distCarList(state, action.data)
        case "ADD":
            return addCarList(state, action.data)
        case "CHECK":
            return getCheck(state, action.data)
        case "ALLCHECK":
            return getAllCheck(state, action.data)
        case "ADDRESS":
            return getAddressList(state, action.data)
        case "ADDMESSAGE":
            return addMessageList(state, action.data);
        case "CLEARMESSAGE":
            return clearMessageList(state)
        case "PULLCMESSAGE":
            return pullCMessage(state, action.data);
        case "CLEARCMESSAGE":
            return clearCMessage(state)
        case "ADDPREPAIDLIST":
            return addprepaidList(state)
        case "ADDUNPAIDLIST":
            return addunpaidList(state)
        case "PULL_lastPathname" :
            return pLastPathname(state, action.data)
        default:
            return {...state };
    }
}

export default createStore(reducer, applyMiddleware(thunk));